package lib

import (
	"encoding/json"
	"log"
	"time"
)

const (
	CodeErr  = -1
	CodeSucc = 10000
)

var ErrCodeMap = map[string]int{"succ": 10000, "err": -1}

//返回值的结构
type ResBody struct {
	Time   int64       `json:"time"`
	Status int         `json:"status"`
	Msg    string      `json:"msg"`
	Data   interface{} `json:"data"`
}

//构造返回值
func (r *ResBody) ResMsg() string {
	r.Time = time.Now().Unix()
	if r.Msg != "" {
		r.Status = ErrCodeMap["err"]
	} else {
		r.Status = ErrCodeMap["succ"]
	}
	j, err := json.Marshal(r)
	if err != nil {
		log.Println("json.Marshal", err)
		return ""
	}
	return string(j)
}
